<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <com1></com1>
        <hr>
        <com2></com2>
    </div>
    <template id="com1">
        <div>
            {{msg}}-----{{mycom1}}
            <button @click="sendCom1">点击传值给com2组件</button>
        </div>
    </template>
    <template id="com2">
        <div>
            {{msg}}------{{mycom2}}
            <button @click="sendCom2">点击传值给com1组件</button>
        </div>
    </template>
    <script src="./js/vue.js"></script>
    <script>
        let eventBus = new Vue();
        let com1 = {
            data() {
                return {
                    msg: "我是com1子组件",
                    mycom1: ""
                }
            },
            methods: {
                sendCom1() {
                    eventBus.$emit("mycom1", this.msg);
                }
            },
            mounted() {
                eventBus.$on("mycom2", val => {
                    this.mycom1 = val
                })
            },
            template: "#com1"
        }
        let com2 = {
            data() {
                return {
                    msg: "我是com2子组件",
                    mycom2: ""
                }
            },
            methods: {
                sendCom2() {
                    eventBus.$emit("mycom2", this.msg)
                }
            },
            mounted() {
                eventBus.$on("mycom1", val => {
                    this.mycom2 = val
                })
            },
            template: "#com2"
        }
        let vm = new Vue({
            el: '#app',
            data: {},
            methods: {},
            components: {
                com1,
                com2
            }
        });
    </script>
</body>

</html>